package logic

import (
	"context"
	"goods_srv/goods"
	"goods_srv/internal/svc"
	"goods_srv/modile/mysql"
	"goods_srv/modile/redis"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGoodsLogic {
	return &CreateGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateGoodsLogic) CreateGoods(in *goods.CreateGoodsRequest) (*goods.CreateGoodsResponse, error) {
	// todo: 新增商品

	g := mysql.GoodsLx{
		GoodsTitle:  in.GoodsTitle,
		GoodsPrice:  in.GoodsPrice,
		GoodsStock:  in.GoodsStock,
		GoodsStatus: int8(in.GoodsStatus),
		GoodsCover:  in.GoodsAvatar,
	}

	err := g.CreateGoods()
	if err != nil {
		logx.Error("数据新增失败", err)
		return nil, err
	}

	//同步虚拟库存
	redis.SyncGoodsStock(int(g.ID), int(g.GoodsStock))

	return &goods.CreateGoodsResponse{GoodsId: int64(g.ID)}, nil
}
